#include <Arduino.h>
#include "ESP8266WiFi.h"
#include "ESP8266WebServer.h"

ESP8266WebServer server(80);
void handleRoot(){
  String html = "<!DOCTYPE html>\n"
                "<html lang=\"en\">\n"
                "<head>\n"
                "    <meta charset=\"UTF-8\">\n"
                "   <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
                "</head>\n"
                "<body>\n"
                "    <h3>局域网控制LED</h3>\n"
                "    <a href=\"/on\">打开LED</a>\n"
                "    <a href=\"/off\">关闭LED</a>\n"
                "</body>\n"
                "</html>";
  server.send(200,"text/html",html);

}

void handleOn(){
  digitalWrite(LED_BUILTIN,LOW);
  server.sendHeader("Location","/");
  server.send(303);
}

void handleOff(){
  digitalWrite(LED_BUILTIN,HIGH);
  server.sendHeader("Location","/");
  server.send(303);
}

void handleNotFound(){
  server.send(404,"text/plain","Not Found");
}

void setup() {
    Serial.begin(9600);
    WiFi.mode(WIFI_AP);    //设置为AP模式,热点模式
    WiFi.softAP("esp8266-wifi","12345678"); //热点wifi用户名密码

    pinMode(LED_BUILTIN,OUTPUT);
    server.begin();
    server.on("/",handleRoot);
    server.on("/on",handleOn);
    server.on("/off",handleOff);
    server.onNotFound(handleNotFound);
  
}

void loop() {
  server.handleClient();
}